package com.dongjiantong.user.serviceImpl;/**
 * Created by cao on 2017/12/1.
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.enums.OrgTypeEnum;
import com.dongjiantong.user.mapper.OrganizationUserMapper;
import com.dongjiantong.user.model.OrganizationUser;
import com.dongjiantong.user.service.OrganizationUserService;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cao
 * @create 2017-12-01 下午1:35
 **/
@Slf4j
@Service(version = "1.0.0")
public class OrganizationUserServiceImpl extends ServiceMybatis<OrganizationUser> implements OrganizationUserService {

    @Resource
    private OrganizationUserMapper organizationUserMapper;

    /**
     * 查询某用户所在组织
     *
     * @param userId
     * @return
     */
    @Override
    public OrganizationUser queryOrganizationByUserId(long userId) {
        OrganizationUser organizationUser = new OrganizationUser();
        organizationUser.setUserId(userId);
        organizationUser.setType(OrgTypeEnum.OrgType_organization.getCode());
        return organizationUserMapper.selectOne(organizationUser);
    }

    /**
     * 查询某机构下的所有用户
     *
     * @param organizationId
     * @return
     */
    @Override
    public List<OrganizationUser> getListByOrgId(Long organizationId) {
        //通用Example查询
        Example example = new Example(OrganizationUser.class);

        //这里给出的条件查询为id=100
        example.createCriteria().andEqualTo("orgId", organizationId);
        return organizationUserMapper.selectByExample(example);
    }

    @Override
    public List<OrganizationUser> selectByUserId(Long userId) {
        return organizationUserMapper.selectByUserId(userId);
    }

    @Override
    public List<Long> userIdList(List<Long> orgIdList) {
        Example example = new Example(OrganizationUser.class);
        example.createCriteria().andIn("orgId", orgIdList);
        List<OrganizationUser> userList = organizationUserMapper.selectByExample(example);
        List list = new ArrayList();
        for (OrganizationUser organizationUser : userList) {
            list.add(organizationUser.getUserId());
        }
        return list;
    }
}
